// pages/user/putInPoint/pointDetail/pointDetail.js
const app = getApp();
import Api from '../../../../utils/http';
// 引入SDK核心类
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');
var key = app.globalData.qqmapsdk;
var qqmapsdk = new QQMapWX({
  key: key
});
var wxCharts = require('../../../../utils/wxcharts.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webSiteUrl: app.globalData.webSiteUrl,
    pointDetails: null,
    lineDetails: null,
    columnDetails: [],
    month: [],
    listHis: [],
    bannerList: [],
    show: false, //显示图集
    follow: 1,
  },
  //扫码开门
  open: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    if (uid.user_type == 1) {
      wx.navigateTo({
        url: '/pages/user/user/userCode/userCode?codeType=2',
      })
    } else {
      wx.navigateTo({
        url: '/pages/user/user/userCode/userCode?codeType=1',
      })
    }

  },
  //快捷领袋
  quick: function () {
    this.getusertext();
  },

  //收运提醒
  remind: function () {
    this.getRemind();
  },
  //关注
  addfollow: function (e) {
    var that = this;
    console.log(that.data);
    that.getFollow(e);
    wx.showToast({
      title: '关注成功',
      icon: 'success',
      duration: 1000
    })
  },
  //取消关注
  celfollow: function (e) {
    var that = this;
    console.log(that.data);
    that.getFollow(e);
    wx.showToast({
      title: '成功取消关注',
      icon: 'success',
      duration: 1000
    })
  },
  //查看图集
  imgSel: function () {
    var show = this.data.show;
    if (show == false) {
      this.setData({
        show: true
      })
    }
  },
  //关闭图集
  close: function () {
    var show = this.data.show;
    if (show == true) {
      this.setData({
        show: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    });
    that.getLaunchData();
    that.getImgSel();
    that.getMonthData();
    that.getColumnData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //关注
  getFollow: function (e) {
    var that = this;
    let uid = wx.getStorageSync('userid')
    let followtype = e.currentTarget.dataset.type;
    var param = {
      uid: uid.id,
      lid: that.data.options.lid,
      type: followtype,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/follow", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        setTimeout(function () {
          that.getLaunchData();
        }, 1000)
      }
    });
  },
  //查看图集接口
  getImgSel: function () {
    var that = this;
    var param = {
      lid: that.data.options.lid,
    }
    console.log(param)
    Api.ajax("/app/Zlaunchpoint/imgSel", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data == null) {
          wx.showToast({
            title: '暂无图集',
            icon: 'none',
            duration: 1000
          })
        }
        that.setData({
          bannerList: res.data
        })
      }
    });
  },
  //提醒收运
  getRemind: function () {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      type: 3,
      lid: that.data.options.lid
    }
    console.log(param)
    Api.ajax("/app/zindex/callRecycle", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '消息已发送',
          icon: 'success'
        })
      } else {
        wx.showToast({
          title: '今日已呼叫',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //调取投放点详情数据
  getLaunchData: function () {

    var that = this;
    let uid = wx.getStorageSync('userid')
    // let {
    //   lid,
    //   longitude,
    //   latitude
    // } = options;
    // var lids = parseInt(lid);
    var param = {
      lid: parseInt(that.data.options.lid),
      longitude: parseFloat(that.data.options.longitude),
      latitude: parseFloat(that.data.options.latitude),
      uid: uid.id,
      user_type: uid.user_type
    };
    console.log(param)
    Api.ajax('/app/Zlaunchpoint/pointDetails', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          pointDetails: res[0]
        })
      }
    });
  },
  //投放月走势数据
  getMonthData: function () {
    var that = this;
    var param = {
      type: 1,
      selType: 1,
      lid: parseInt(that.data.options.lid),
    };
    console.log(param)
    Api.ajax('/app/Zmanage/launchData', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          lineDetails: res.data,
        })
        that.canvasLine();

      }
    });
  },
  //投放分类走势
  getColumnData: function () {
    var that = this;
    var param = {
      lid: parseInt(that.data.options.lid),
      type: 1,
      selType: 4
    };
    console.log(param)
    Api.ajax("/app/Zmanage/dataForm", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          columnDetails: res.data,
        })
        that.canvasColumn();

      }
    });
  },
  //cavns数据
  canvasLine: function () {
    var that = this;
    // 历史数据
    let lineDetails = that.data.lineDetails;
    var month = [];
    var datas = [];
    // console.log(lineDetails)

    for (var i in lineDetails) {

      var obj = {
        month: lineDetails[i].month,
        data: lineDetails[i].weight
      }
      month = month.concat(obj.month);
      //console.log(month);
      datas = datas.concat(obj.data);
      // console.log(datas);
    }
    that.setData({
      month: month,
      datas: datas,
    });
    // 历史数据视图
    new wxCharts({
      canvasId: 'lineWeek',
      type: 'line',
      categories: month,
      animation: true,
      background: '#ffffff',
      series: [{
        name: '3月',
        data: datas,
        color: '#00c188',
        format: function (val, name) {
          return val + 'kg';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        min: 0,
      },
      width: 360,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      legend: false,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //分类信息
  canvasColumn: function () {
    var that = this;
    let columnDetails = that.data.columnDetails;
    var title = [];
    var weights = [];
    for (var i in columnDetails) {
      var obj = {
        title: columnDetails[i].title,
        weights: columnDetails[i].weight,
      }
      title = title.concat(obj.title);
      //console.log(title);
      weights = weights.concat(obj.weights);
      // console.log(weights);
    }
    that.setData({
      title: title,
      weights: weights,
    });
    //分类信息
    new wxCharts({
      canvasId: 'lineCanvas',
      type: 'column',
      categories: title,
      
      series: [{
        names: '综合纸',
        data: weights,
        format: function (val, name) {
          return val + 'kg';
        }
      }],
      yAxis: {
        min: 0,
      },
      extra: {
        column: {
          width: 25
        }
      },
      width: 350,
      height: 300,
      dataLabel: true,
      legend: false,
    });
  },
  //个人信息
  getusertext: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data.user_type==1) {
          if (res.data.verification != 0) {
            if (res.data.recyc_bag_num == 0) {
              wx.showModal({
                title: '提示',
                content: '数量不足，请先购买',
                confirmText: '确定购买',
                showCancel: false,
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/user/user/userRecyclingBuy/userRecyclingBuy',
                    })
                  }
                }
              })
            } else {
              wx.navigateTo({
                url: '/pages/user/user/userRecyclingGet/userRecyclingGet',
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: '当前用户未认证，点击确定前往认证',
              confirmText: '确定',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/user/user/mobileAuth/mobileAuth',
                  })
                }
              }
            })
          }
        }else if(res.data.user_type==2){
          if (res.data.recyc_bag_num == 0) {
            wx.showToast({
              title: '数量不足，请联系单位管理员',
              icon:'none',
              duration:2000
            })
          } else {
            wx.navigateTo({
              url: '/pages/user/user/userRecyclingGet/userRecyclingGet',
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})